package com.dragon.mmochat.message;

import java.util.Arrays;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_1_31S extends SkymobiProtocolMessage {
	int hallState;
	int roleId;

	public Mmochat_1_31S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_1_31);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		out.putUnsignedShort(hallState);
		out.putInt(MmochatMain.getServerVersion());
		out.putInt(roleId);
		out.put((byte) MmochatConstant.maxMapLineNum);
		Integer[] linePlayerNum = new Integer[MmochatConstant.maxMapLineNum];
		Arrays.fill(linePlayerNum, 0);
		try {
			for (MmochatMap map : MmochatMapService.maps.values()) {
				if (map != null) {
					for (int i = 0; i < MmochatConstant.maxMapLineNum; i++) {
						linePlayerNum[i] += map.getLineRoleMap(i).size();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (int i = 0; i < MmochatConstant.maxMapLineNum; i++) {
			try {
				out.put((byte) MmochatUtil.getLineHotType(linePlayerNum[i])
						.getValue());
			} catch (Exception e) {
				out.put((byte) 0);
			}
		}
	}

	public void setHallState(int hallState) {
		this.hallState = hallState;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

}